<!--master-->
<table>
	<caption>
		<label for="recordType"><g:message code="b2b.salesOrder.recordType" default="Record Type" /></label>:
		${salesOrderInstance?.recordType?.encodeAsHTML()}
		<g:hiddenField name="recordType.id" value="${salesOrderInstance?.recordType?.id}" />
	</caption>
    <tbody>
        <tr class="prop">
        	<!-- dealer -->
            <td class="name">
                <label for="dealer"><g:message code="b2b.salesOrder.dealer" default="Dealer" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'dealer', 'errors')}">
                <g:textField name="dealerInput" value="${salesOrderInstance?.dealer?.encodeAsHTML()}"/>
                <g:hiddenField name="dealer.id" value="${salesOrderInstance?.dealer?.id}" />
            </td>
            <!-- department -->
            <td class="name">
                <label for="department"><g:message code="b2b.salesOrder.department" default="Department" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'department', 'errors')}">
                <span id="department">
                    ${include(controller : 'salesOrderAjax', action : 'getDealerDepartment', params : [dealerSerialNumber : salesOrderInstance?.dealer?.serialNumber] )}
                </span>
            </td>
            <!-- order date -->
            <td class="name">
                <label for="orderDate"><g:message code="b2b.salesOrder.orderDate" default="Order Date" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'orderDate', 'errors')}">
            	<g:set var="orderDate" value="${salesOrderInstance.orderDate?salesOrderInstance.orderDate:new Date() }"></g:set>
                ${formatDate(format:'yyyy-MM-dd', date : orderDate) }
                <g:hiddenField name="orderDate_year" value="${formatDate(format:'yyyy', date : orderDate) }" />
                <g:hiddenField name="orderDate_month" value="${formatDate(format:'MM', date : orderDate) }" />
                <g:hiddenField name="orderDate_day" value="${formatDate(format:'dd', date : orderDate) }" />
            </td>
		</tr>  
		
		<tr class="prop">
            <!-- industry -->
            <td class="name">
            	<label for="industry"><g:message code="b2b.salesOrder.industry" default="Industry" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'industry', 'errors')}">
                <g:textField name="industryInput" value="${salesOrderInstance?.industry?.encodeAsHTML()}" />
                <g:hiddenField name="industry.id" value="${salesOrderInstance?.industry?.id}"  />
			</td>
        	<!-- project -->
            <td class="name">
				<label for="project"><g:message code="b2b.salesOrder.project" default="Project" /></label>
			</td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'project', 'errors')}">
				<g:textField name="project" value="${salesOrderInstance?.project }"/>
			</td>
        	<!-- account serial number -->
            <td class="name">
				<label for="accountSerialNumber"><g:message code="b2b.salesOrder.accountSerialNumber" default="Account Order Number" /></label>
			</td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'accountSerialNumber', 'errors')}">
				<g:textField name="accountSerialNumber" value="${salesOrderInstance?.accountSerialNumber }"/>
			</td>
		</tr>
		<tr class="prop">
			<!-- sales man -->
            <td class="name">
                <label for="salesMan"><g:message code="b2b.salesOrder.salesMan" default="Sales Man" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesMan', 'errors')}">
                <span id="salesMan">${salesOrderInstance?.salesMan?.encodeAsHTML()}</span>
                <g:hiddenField name="salesMan.id" value="${salesOrderInstance?.salesMan?.id}" />
            </td>
        	<td class="name">&nbsp;</td>
        	<td class="value">&nbsp;</td>
        	<td class="name">&nbsp;</td>
        	<td class="value">&nbsp;</td>
		</tr>

        <tr class="prop">
            <td class="name">
                <label for="address1"><g:message code="b2b.salesOrder.address1" default="Address(1)" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'address1', 'errors')}" colspan="5">
                <g:textArea name="address1" maxlength="72" value="${salesOrderInstance?.address1}" rows="1" />
            </td>
        </tr> 

        <tr class="prop">
            <td class="name">
                <label for="address2"><g:message code="b2b.salesOrder.address2" default="Address(2)" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'address2', 'errors')}" colspan="5">
                <g:textArea name="address2" maxlength="72" value="${salesOrderInstance?.address2}" rows="1"/>
            </td>
        </tr> 
		
        <tr class="prop">
        	<!-- description -->
            <td class="name">
                <label for="description"><g:message code="b2b.salesOrder.description" default="Desc" /></label>
            </td>
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'description', 'errors')}" colspan="5">
                <g:textArea name="description" cols="40" rows="5" value="${salesOrderInstance?.description}" />
            </td>
        </tr>		
		   
	</tbody>
</table>
<!--end master-->
<!--detail-->
<table>
    <tbody id="salesOrderDetailFooter">
    	<tr>
    		<td>
    			<label for="searchProductNumber"><g:message code="b2b.salesOrderDetail.search.product" default="Search Product" />:</label>
		        <g:textField name="searchProductNumber" style="width:300px;"/>
		        <g:hiddenField name="searchProductId"/>
		        <span id="searchProductName"></span>
		        ,<span id="searchProductStandard"></span>
		        ,<span id="searchProductPrice"></span>
			</td>
    	</tr>
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th><g:message code="b2b.salesOrderDetail.serialNumber" default="Serial Number" /></th>
            <th><g:message code="b2b.salesOrderDetail.openDetail" default="Open Detail" /></th>
            <th><g:message code="b2b.salesOrderDetail.product" default="Product" /></th>
            <th><g:message code="b2b.product.name" default="Product Name" /></th>
            <th><g:message code="b2b.product.standard" default="Standard" /></th>
            <th><g:message code="b2b.salesOrderDetail.category" default="Category" /></th>
            <th>
                <g:message code="b2b.salesOrderDetail.price" default="Price" />
                (<g:message code="b2b.category.isAllowZeroPrice" default="Allow Zero Price?" />)
            </th>
            <th><g:message code="b2b.salesOrderDetail.discount" default="Discount" /></th>
            <th>
                <g:message code="b2b.salesOrderDetail.quantity" default="Order Quantity" />
                (<g:message code="b2b.bomDetail.dosage" default="Dosage" />,
                <g:message code="b2b.bomDetail.quota" default="Quota" />)
            </th>
            <th>
                <g:message code="b2b.salesOrderDetail.deliveryLimitation" default="Delivery Limitation" />
                (<g:message code="b2b.category.deliveryCycle" default="Delivery Cycle" />)
            </th>
            <th><g:message code="b2b.salesOrderDetail.amount" default="Amount" /></th>
        </tr>
    </thead>
    <tbody id="salesOrderDetailList">
    <g:each in="${salesOrderInstance.salesOrderDetails}" status="i" var="salesOrderDetailInstance">
        <!-- rowspan -->
        <g:set var="rowspan" value="${0}" />
       	<g:if test="${salesOrderDetailInstance?.salesOrderDetailDetails?.size() > 0}">
            <g:set var="rowspan" value="${salesOrderDetailInstance?.salesOrderDetailDetails?.size() + 1}" />
       	</g:if>
       	
        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}" childs="${salesOrderDetailInstance?.serialNumber}">
            <!--serial number-->
        	<g:if test="${rowspan > 0}"><td rowspan="${rowspan}" name="serialNumber"></g:if><g:else><td name="serialNumber"></g:else>
            	<span>${fieldValue(bean: salesOrderDetailInstance, field: "serialNumber")}</span>
            	<g:hiddenField name="salesOrderDetails[${i}].serialNumber" value="${salesOrderDetailInstance?.serialNumber}"/>
            	<br/>
            	<a href="#addSalesOrderDetailLink" delete="delete">${message(code: 'default.button.delete', default: 'Delete')}</a>
            </td>
            <!--open detail-->
            <td name="openDetail">
            	<g:checkBox name="salesOrderDetails[${i}].openDetail" value="${salesOrderDetailInstance?.openDetail}" onclick="return false;"/>
            </td>
            <!--product.id-->
            <td name="product.id">
            	${fieldValue(bean: salesOrderDetailInstance, field: "product")}
            	<g:hiddenField name="salesOrderDetails[${i}].product.id" value="${salesOrderDetailInstance?.product?.id}"/>
            </td>
            <!--product.name-->
            <td name="product.name">${fieldValue(bean: salesOrderDetailInstance, field: "product.name")}</td>
            <!--product.standard-->
            <td name="product.standard">${fieldValue(bean: salesOrderDetailInstance, field: "product.standard")}</td>
            <!--category-->
            <td name="category">
            	${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailInstance?.product?.serialNumber] )}
            </td>
            <!--price-->
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.price', 'errors')} number" name="price">
            	<g:textField name="salesOrderDetails[${i}].price" value="${formatNumber( number : salesOrderDetailInstance?.price, format:'###,##0.000000')}" readonly="readonly"/>
                <span>
            		(<g:include controller="salesOrderAjax" action="isAllowZeroPrice" params="[product:salesOrderDetailInstance?.product?.serialNumber]"/>)
            	</span>
            </td>
            <!--discount-->
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.discount', 'errors')} number" name="discount">
            	<g:textField name="salesOrderDetails[${i}].discount" value="${formatNumber( number : salesOrderDetailInstance?.discount, format:'###,##0.0000')}" readonly="readonly" style="width:55px;"/>
            </td>
            <!--quantity-->
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.quantity', 'errors')} number" name="quantity" >
            	<g:textField name="salesOrderDetails[${i}].quantity" value="${formatNumber( number : salesOrderDetailInstance?.quantity, format:'###,##0.00')}"/>
            </td>
            <!--delivery limitation-->
            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.deliveryLimitation', 'errors')}" name="deliveryLimitation">
            	<g:jqDatePicker name="salesOrderDetails[${i}].deliveryLimitation" value="${salesOrderDetailInstance?.deliveryLimitation}"/>
            	<span>
            		<g:include controller="salesOrderAjax" action="deliveryCycle" params="[product:salesOrderDetailInstance?.product?.serialNumber]"/>
            	</span>
            </td>
            <!--amount-->
            <td class="value number" name="amount" >
                <span>
                    <g:formatNumber number="${salesOrderDetailInstance?.amount?:0}" format="###,##0.00" />
                </span>
                <g:hiddenField name="salesOrderDetails[${i}].amount" value="${salesOrderDetailInstance?.amount?:0}"/>
            </td>
        </tr>
	    <g:each in="${salesOrderDetailInstance.salesOrderDetailDetails}" status="j" var="salesOrderDetailDetailInstance">
	        	
	        <tr class="${(j % 2) == 0 ? 'even' : 'old'}" parent="${salesOrderDetailInstance?.serialNumber}">
                <!--serial number-->
	        	<td name="serialNumber">
	        		(+)${fieldValue(bean: salesOrderDetailDetailInstance, field: "serialNumber")}
	            	<g:hiddenField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].serialNumber" value="${salesOrderDetailDetailInstance?.serialNumber}"/>
	        	</td>
                <!--product.id-->
	            <td name="product.id">
	            	${fieldValue(bean: salesOrderDetailDetailInstance, field: "product")}
	            	<g:hiddenField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].product.id" value="${salesOrderDetailDetailInstance?.product?.id}"/>
	            </td>
                <!--product.name-->
	            <td name="product.name">${fieldValue(bean: salesOrderDetailDetailInstance, field: "product.name")}</td>
                <!--product.standard-->
	            <td name="product.standard">${fieldValue(bean: salesOrderDetailDetailInstance, field: "product.standard")}</td>
                <!--category-->
	            <td name="category">
                    ${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailDetailInstance?.product?.serialNumber] )}
                </td>
                <!--price-->
	            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.salesOrderDetailDetails.price', 'errors')} number" name="price">
	            	<g:textField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].price" value="${formatNumber( number : salesOrderDetailDetailInstance?.price, format:'###,##0.000000')}" readonly="readonly"/>
                    <span>
            		(<g:include controller="salesOrderAjax" action="isAllowZeroPrice" params="[product:salesOrderDetailDetailInstance?.product?.serialNumber]"/>)
            	    </span>
	            </td>
                <!--discount-->
	            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.salesOrderDetailDetails.discount', 'errors')} number" name="discount">
	            	<g:textField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].discount" value="${formatNumber( number : salesOrderDetailDetailInstance?.discount, format:'###,##0.0000')}" readonly="readonly" style="width:55px;"/>
	            </td>
                <!--quantity-->
	            <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.salesOrderDetailDetails.quantity', 'errors')} number" name="quantity">
	            	<g:textField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].quantity" value="${formatNumber( number : salesOrderDetailDetailInstance?.quantity, format:'##0.000')}" readonly="readonly"/>
	            	<span>
	            		<g:include controller="salesOrderAjax" action="dosage" params="[masterProduct:salesOrderDetailInstance?.product?.serialNumber,detailProduct:salesOrderDetailDetailInstance?.product?.serialNumber]"/>
	            	</span>
                    <span>,</span>
                    <span>
	            		<g:include controller="salesOrderAjax" action="quota" params="[masterProduct:salesOrderDetailInstance?.product?.serialNumber,detailProduct:salesOrderDetailDetailInstance?.product?.serialNumber]"/>
	            	</span>
	            </td>
                <!--blank-->
	            </td>&nbsp;<td>
                <!--amount-->
	            <td class="value number" name="amount">
                    <span>
					    <g:formatNumber number="${salesOrderDetailDetailInstance?.amount?:0}" format="###,##0.00" />
                    </span>
                    <g:hiddenField name="salesOrderDetails[${i}].salesOrderDetailDetails[${j}].amount" value="${salesOrderDetailDetailInstance?.amount?:0}"/>
	            </td>
	        </tr>
	    </g:each>
    </g:each>
    </tbody>
    <tfoot>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.amount.sum" default="Total Amount" /></label>
            </td>
            <td name="totalAmount">
            	<g:formatNumber number="${salesOrderInstance?.salesOrderDetails*.amount?.sum() ?: 0}" format="###,##0.00" />
            </td>
            <td>&nbsp;</td>
        </tr>
    </tfoot>
</table>
<!--master-->
<g:render template="/b2b/salesOrder/script" />
<g:render template="/b2b/salesOrder/script_220" />
